package com.xuan.bkm.service.user.admin.service.impl;

import com.xuan.bkm.constants.RespConstants;
import com.xuan.bkm.constants.SystemConstants;
import com.xuan.bkm.entity.Admin;

import com.xuan.bkm.exception.security.AvildUserException;
import com.xuan.bkm.exception.security.UserNameIsEmptyException;
import com.xuan.bkm.exception.security.UsernameNotFoundException;
import com.xuan.bkm.service.user.admin.dao.IAdminDao;
import com.xuan.bkm.service.user.admin.service.IAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

@Service
@Transactional
public class AdminServiceImpl implements IAdminService {

    @Autowired
    private IAdminDao iad;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Admin login(String username, String password) {
        if(null==username||"".equals(username))
            throw new UserNameIsEmptyException(RespConstants.USERNAME_IS_EMPYTY);

        if(!stringRedisTemplate.opsForSet().isMember(SystemConstants.REDIS_ADMIN_NUMS,username))
            throw new UsernameNotFoundException(RespConstants.USERNAME_NOT_FOUND);

        Admin admin=iad.findByNumAndPassword(username, password);

        if(admin==null)throw new AvildUserException(RespConstants.INVAILD_USER);


        return admin;
    }
}
